<template>
  <!-- logo返回首页 -->
  <!-- <div style="display: flex;padding: 1px 0;">
      <div style="width: 220px" class="app-logo" @click="toHome">
          <svg-icon icon="logo" height="48px" width="48px" />
          <span
              style="font-family: FangSong,sans-serif;font-size: 18px;font-weight: bolder;margin-left: 0px;color: #009983;">
              核医学全身骨显像数据库系统
          </span>
      </div>
  </div> -->
  <el-header>
    <div class="top-head custom-space-between">
      <div class="app-logo">
        <svg-icon icon="logo2" height="64px" width="400px" @click="toHome"/>
      </div>
      <div class="custom-space-between" style="flex-wrap: nowrap; align-items: center; margin-right: 80px">
        <el-menu :default-active="defaultActive()" :router="true" :ellipsis="false" mode="horizontal"
                 @select="handleMenuSelect" active-text-color="#36CDFD" class="header-menu" style="width: 1.5rem">
          <el-menu-item index="/">
            <span class="menu-label">首页</span>
          </el-menu-item>
          <el-menu-item index="/intro">
            <span class="menu-label">科室概况</span>
          </el-menu-item>
          <el-menu-item index="/team">
            <span class="menu-label">研究团队</span>
          </el-menu-item>
          <el-menu-item index="/case">
            <span class="menu-label">样例列表</span>
          </el-menu-item>
        </el-menu>

        <LoginDialog v-if="!isLogin" class="login-btn">
          <svg-icon icon="user-bold" height="0.1rem" width="0.1rem" />
          登录
        </LoginDialog>

        <el-dropdown v-else :show-timeout="70" :hide-timeout="50" :teleported="false" class="info-func">
          <span class="user-link">
            <span class="avatar">
              <svg-icon icon="user-bold" height="0.1rem" width="0.1rem" />
            </span>
            {{ user.nickName || user.userName || '' }}
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item v-if="isManager" @click="toManage">后台管理</el-dropdown-item>
              <el-dropdown-item @click="loginOut">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <div class="header-bottom">
      <span class="left" />
      <span class="right" />
    </div>
  </el-header>
</template>

<script lang="ts">
import {Options} from "vue-class-component";
import BasePage from "../BasePage";
import {useRoute, useRouter} from 'vue-router'
import {useUser} from '@/store/user';
import {ElMessageBox, ElMessage} from 'element-plus';
import LoginDialog from "@/components/LoginDialog.vue";
import { storeToRefs } from 'pinia'
import {sleep} from "@/utils";
import {ADMIN_TYPE, ROLE_TYPE} from "@/utils/Constant";

@Options({
  name: 'UserHeader',
  components: { LoginDialog }
})
export default class UserHeader extends BasePage {

  router = useRouter()
  route = useRoute()
  //登录相关
  // user = JSON.parse(sessionStorage.getItem('userInfo')) || {}
  user = storeToRefs(useUser())?.userInfo
  isManager = storeToRefs(useUser())?.isManager

  mounted() {
    console.log()
  }

  defaultActive = () => '/' + (this.route.path.split('/')[1] || '')

  toHome = () => this.router.push('/')

  handleMenuSelect(index) {
    if ("/case" === index) {
      this.user = JSON.parse(sessionStorage.getItem('userInfo')) ? JSON.parse(sessionStorage.getItem('userInfo')) : {}
      if (this.user.nickName) {
        //nothing
      } else {
        ElMessage.error('请登录后查看')
        this.router.push('/')
      }
    }
  }

  //注销登录信息
  async loginOut() {
    this.loading = true
    await sleep(500)
    window.sessionStorage.clear();
    this.user = {}
    this.loading = false
    await this.router.push('/')
    ElMessage.success('已退出系统！')
  };

  toManage() {
    this.router.push('/backstage')
  }

  get isLogin() {
    return !!this.user.userId
  }
}
</script>
<style lang='scss' scoped>
.el-header {
  z-index: 12;
  width: 100%;
  height: 108px;
  position: fixed;
  border-radius: 0;
  background-color: transparent;
  --el-header-padding: 0;
  line-height: 50px;

  .top-head {
    height: 96px;
    opacity: 0.85;
    background: #040E1E;
  }
  .header-bottom {
    width: 60%;
    margin: auto;
    opacity: 0.85;
    position: relative;
    border: 12px solid transparent;
    border-top-color: #040E1E;
  }


  .app-logo {
    cursor: pointer;
    display: flex;
    padding: 0 80px;
    box-sizing: border-box;

    .svg-icon {
      display: flex;
      align-items: center;
      :deep(svg) {
        width: 400px;
        height: 64px;
      }
    }
  }
}


.el-menu.header-menu {
  --el-menu-bg-color: transparent;
  width: 150px;
  display: flex;
  justify-content: space-between;
  border-bottom: none !important;

  .el-menu-item {
    padding: 14px 0px;
    border-bottom: none !important;
    background-color: transparent !important;
    line-height: 1;
    margin-right: 24px;

    &:not(.is-active) .menu-label {
      color: white;
    }
    .menu-label {
      display: flex;
      justify-content: center;
      min-width: 40px;
      font-size: 18px;
      margin-right: 0;
      position: relative;

      &:after {
        right: -12px;
        content: '';
        height: 16px;
        position: absolute;
        border-right: 1px solid #505762;
      }
    }

    &.is-active:after {
      height: 0;
      left: 0;
      right: 0;
      content: '';
      bottom: -1px;
      display: block;
      border: 4px solid transparent;
      border-bottom-color: currentColor;
      position: absolute;
    }
  }

  .personal-card {
    .personal-card__header {
      background-image: url('@/assets/images/personal-card.png');
      color: #fff;
      display: flex;
      padding: 15px 20px;
      height: 80px;
      box-sizing: border-box;
    }

    .personal-menu-item {
      cursor: pointer;
      padding: 12px 20px;
      box-sizing: border-box;
      display: inline-flex;
      justify-content: space-between;
      width: 100%;
      line-height: 16px;
      margin: 2px 0;
    }

    &:hover {
      color: var(--el-color-primary);
    }

    > span {
      display: inline-flex;
    }
  }
}

.login-btn {
  width: 140px;
  height: 40px;
  display: flex;
  color: white;
  font-size: 16px;
  margin-left: 80px;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;

  &:before, &:after {
    width: 100%;
    height: 100%;
    right: 0;
    bottom: 0;
    content: '';
    z-index: -1;
    position: absolute;
    background-color: #185ECA;
    transform: skewX(-45deg);
  }

  &:after {
    width: 18px;
    height: 18px;
    z-index: 1;
    bottom: -6px;
    border: 6px solid #040E1E;
    background-color: #36CDFD;
    transform: skewX(-45deg);
  }

  &:hover:before {
    background-color: #0E3879;
  }
}

.info-func {
  margin-left: 80px;
}
.el-dropdown .user-link {
  outline: none;
}
.user-link {
  display: flex;
  color: #36CDFD;
  cursor: default;
  position: relative;
  align-items: center;

  .avatar {
    height: 30px;
    width: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 14px;
    margin-right: 12px;
    color: #1E2735;
    background-color: #36CDFD;
  }

  &:after {
    top: 50%;
    right: -12px;
    content: '';
    position: absolute;
    border: 5px solid transparent;
    border-top-color: #36CDFD;
  }
}
</style>